const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
// date  1990-02-01
const dateFormat = function() {
  const date = new Date();
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('/')
} 

function setTime(timestr){
  var date = new Date(timestr);
  var Y = date.getFullYear();
  var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
  var D = change(date.getDate());
  var h = change(date.getHours());
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return Y + '/' + M + '/' + D;
}

function setWeek(date){
  var date = new Date(date);
  var weekday=["周日","周一","周二","周三","周四","周五","周六"];
  var day = date.getDay();
  return weekday[day]
}

function change(t) {
  if (t < 10) {
      return "0" + t;
  } else {
      return t;
  }
}

//正则验证
function validation (data,type) {
  let str = ''
  if(type == 'phone')
  {
    str = /^1\d{10}$/
  }
  else if(type == 'idcard')
  {
    str = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  }
  else if(type == 'name')
  {
    str = /^[\u4E00-\u9FA5A-Za-z]+$/
  }
  else if(type == 'number')
  {
    // 验证非负浮点数
    str = /^(\-|\+)?\d+(\.\d+)?$/
  }
  else if(type == 'alphanumeric')
  {
    // 验证由数字和26个英文字母组成的字符串
    str = /^[A-Za-z0-9]+$/
  }else if(type == 'comma'){
    //验证是否英文逗号
    str = /^(\d+,?)+$/
  }else if(type == 'space'){
    str = /^[/s+]+$/
  }
  if (str.test(data)) {
    return true
  }
  else{
    return false
  }
}

function showModelAll(title,content,callback){
  wx.showModal({
      title: title,
      content: content,
      success(res) {
          if (res.confirm) {
              callback()
          } else if (res.cancel) {
              // console.log('用户点击取消')
          }
      }
  })
}

function showModal(title,content,callback){
  wx.showModal({
      title: title,
      content: content,
      showCancel:true,
      success (res) {
        callback(res)
      }
  })
  return
}

function showToast(title,icon,duration){
  wx.showToast({
    title: title,
    icon: icon,
    duration: duration,
  })
}

function setNavBarColor(fontColor,bgColor){
  wx.setNavigationBarColor({
    frontColor:fontColor,
    backgroundColor:bgColor
  })
}
function setNavBarTitle(title){
  wx.setNavigationBarTitle({
    title:title
  })
}

function getAjax(url,callback,data,failback){
  wx.request({
    url:url,
    data:data,
    method:'get',
    success(res){
      callback(res)
    },
    fail(err){
      failback(err)
    }
  })
}
function postAjax(url,callback,data,failback){
  wx.request({
    url:url,
    data:data,
    header:{
      'content-type': 'application/x-www-form-urlencoded'
    },
    success(res){
      callback(res)
    },
    fail(err){
      failback(err)
    }
  })
}
function beforeAfterMonth() {
  var today = new Date();
  var t_year = today.getFullYear();
  var t_month = today.getMonth() + 1;
  var t_day = today.getDate();
  var start=new Date(today.getTime()-31 * 24 * 3600 * 1000);
  var s_year = start.getFullYear();
  var s_month = start.getMonth() + 1;
  var s_day = start.getDate();
  var end=new Date(today.getTime()+31 * 24 * 3600 * 1000);
  var e_year = end.getFullYear();
  var e_month = end.getMonth() + 1;
  var e_day = end.getDate();
  return {
            before: [s_year, s_month, s_day].map(formatNumber).join('/'), 
            after: [e_year, e_month, e_day].map(formatNumber).join('/'),
            today: [t_year, t_month, t_day].map(formatNumber).join('/')
          }
}
// const formatNumber = n => {
//   n = n.toString()
//   return n[1] ? n : '0' + n
// }
// // date  1990-02-01
// const dateFormat = function() {
//   const date = new Date();
//   const year = date.getFullYear()
//   const month = date.getMonth() + 1
//   const day = date.getDate()
//   return [year, month, day].map(formatNumber).join('/')
// } 

//设置时间
// function setDate(time){
//   var isEnd = false
//   var endTime = (new Date(String(setTime(new Date()) + ' ' + time +':00')).getTime()) /1000;//倒计时的结束时间
//   var nowTime = (new Date(formatTime(new Date())).getTime()) /1000
//   // console.log(endTime)
//   var total_micro_second = endTime - nowTime
//   if (total_micro_second <= 0) {
//     isEnd = true
//   }
//   return isEnd
// }

function setDate(startDate,endDate){
  var curDate = Number((new Date(formatTime(new Date())).getTime()) /1000) 
  var start = Number((new Date(String(setTime(new Date()) + ' ' + startDate)).getTime()) /1000)
  var end = Number((new Date(String(setTime(new Date()) + ' ' + endDate)).getTime()) /1000) 
  if(curDate >= start && curDate <= end){
    return true
  }
  return false
}

module.exports = {
  formatTime: formatTime,
  setTime: setTime,
  validation: validation,
  showModelAll: showModelAll,
  showModal: showModal,
  showToast: showToast,
  setNavBarColor:setNavBarColor,
  setNavBarTitle:setNavBarTitle,
  getAjax: getAjax,
  postAjax: postAjax,
  setWeek:setWeek,
  setDate: setDate,
  dateFormat: dateFormat,
  beforeAfterMonth:beforeAfterMonth
}
